package com.cms.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;

import com.action.DefaultAction;
import com.cms.model.TerraceInfo;
import com.cms.service.TerraceInfoService;
import com.cms.service.TerraceInfoServiceImpl;

/**
 * @author luoyh
 * @date 2014‎-‎3‎-24
 */
public class TerraceInfoAction extends DefaultAction {

	private static final long serialVersionUID = 1L;
	private Logger logger;
	private TerraceInfoService service  = new TerraceInfoServiceImpl();
	private List<TerraceInfo> list;
	private TerraceInfo model;
	private String txt_keyword;
	private String msg;
	public TerraceInfoAction() {
		logger = Logger.getLogger(this.getClass());
	}
		
	public String execute()
	{
		return SUCCESS;
	}

	public String list()
	{
		list = service.getTerraceInfos(txt_keyword);
		return SUCCESS;
	}
	
	public String show()
	{
		if(msg.equals("update"))
		{
			model = service.getTerraceInfo(model.getId());
		}
		else
		{
			model = new TerraceInfo();
			model.setId(-1);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Calendar calendar = Calendar.getInstance();			
			calendar.add(Calendar.YEAR, 1);
			model.setContractPeriod(df.format(calendar.getTime()));
		}

		return SUCCESS;
	}
	
	public String save()
	{
		if(msg.equals("insert"))
		{
			if(service.insertTerraceInfo(model))
			{
				msg="平台管理：添加平台成功！！";
			}
			else
			{
				msg="平台管理：对不起，添加平台可能存在问题，请稍候重试一下！！";
			}
		}
		else if(msg.equals("update"))
		{
			if(service.updateTerraceInfo(model))
			{
				msg="平台管理：更新平台成功！！";
			}
			else
			{
				msg="平台管理：对不起，更新平台可能存在问题，请稍候重试一下！！";
			}
		}
		else if(msg.equals("del"))
		{
			if(service.deleteTerraceInfo(model))
			{
				msg="平台管理：删除平台成功！！";
			}
			else
			{
				msg="平台管理：对不起，删除平台可能存在问题，请稍候重试一下！！";
			}
		}
		return SUCCESS;
	}
	public String chkCode(){
		getResponse().setCharacterEncoding("utf-8");  
        PrintWriter pw=null;  
        try {  
        	String code = new String(model.getCode().getBytes("ISO8859-1"),"UTF-8");
            pw = getResponse().getWriter();  
            List<TerraceInfo> l = service.getTerraceInfos();
            for (TerraceInfo t : l) {
				if(code.equals(t.getCode())){
					pw.write("1"); 
					break;
				}
			}
        } catch (IOException e) {  
            System.out.println(e.getMessage());
        }  
        pw.flush();  
        pw.close(); 
		return null;
	}
	public TerraceInfoService getService() {
		return service;
	}


	public void setService(TerraceInfoService service) {
		this.service = service;
	}


	public List<TerraceInfo> getList() {
		return list;
	}


	public void setList(List<TerraceInfo> list) {
		this.list = list;
	}


	public TerraceInfo getModel() {
		return model;
	}


	public void setModel(TerraceInfo model) {
		this.model = model;
	}


	public String getMsg() {
		return msg;
	}


	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getTxt_keyword() {
		return txt_keyword;
	}

	public void setTxt_keyword(String txt_keyword) {
		this.txt_keyword = txt_keyword;
	}
	
	
}
